package ljl.alg.jianzhioffer.round3.medium;

import commons.TreeNode;

public class _26_isSubStructure {

    public boolean isSubStructure(TreeNode A, TreeNode B) {
        return A != null && B != null
                && (contains(A, B) || isSubStructure(A.left, B) || isSubStructure(A.right, B));
    }

    boolean contains(TreeNode a, TreeNode b) {
        if (b == null) return true;
        if (a == null || a.val != b.val) return false;
        return contains(a.left, b.left) && contains(a.right, b.right);
    }
}
